package kau.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Customer;

import org.apache.ibatis.session.SqlSession;

public class CustomerDAOImpl implements CustomerDAOIF {

	DBUtil database = null;
	private static CustomerDAOImpl instance;

	private CustomerDAOImpl() {
		database = DBUtil.getInstance();
	}

	public static CustomerDAOImpl getInstance() {
		if (instance == null)
			instance = new CustomerDAOImpl();
		return instance;
	}

	@Override
	public void insert(Customer customer) throws DuplicationException {
		if (isExist(customer.getCustId()))
			throw new DuplicationException();

		SqlSession session = database.getSession();
		try {
			session.insert("dbConfig.CustomerMapper.insert", customer);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public void update(Customer customer) throws RecordNotFoundException {
		if (!isExist(customer.getCustId()))
			throw new RecordNotFoundException(customer.getCustId());

		SqlSession session = database.getSession();

		try {
			session.update("dbConfig.CustomerMapper.update", customer);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

	}

	@Override
	public void delete(String custId, String password)
			throws RecordNotFoundException {
		if (!isExist(custId))
			throw new RecordNotFoundException(custId);
		SqlSession session = database.getSession();
		HashMap<String, String> info = new HashMap<String, String>();
		info.put("custId", custId);
		info.put("password", password);
		System.out.println(custId + " " + password);
		try {
			session.delete("dbConfig.CustomerMapper.delete", info);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public Customer getCustomer(String custId) throws RecordNotFoundException {
		SqlSession session = database.getSession();
		Customer cust = null;
		try {
			cust = session.selectOne("dbConfig.CustomerMapper.getCustomer",
					custId);
		} finally {
			if (session != null)
				session.close();
		}
		if (cust == null)
			throw new RecordNotFoundException(custId);
		return cust;
	}
	@Override
	public ArrayList<Customer> noPermissionCustList() {
		ArrayList<Customer> custList = new ArrayList<Customer>();
		SqlSession session = database.getSession();
		try {
			custList = (ArrayList) session
					.selectList("dbConfig.PermissionMapper.showNoPermissionCust");
		} finally {
			if (session != null)
				session.close();
		}
		return custList;
	}

	@Override
	public ArrayList<Customer> getAllCustomers() {
		ArrayList<Customer> custList = new ArrayList<Customer>();
		SqlSession session = database.getSession();
		try {
			custList = (ArrayList) session
					.selectList("dbConfig.CustomerMapper.getAllCustomers");
		} finally {
			if (session != null)
				session.close();
		}
		return custList;
	}

	private boolean isExist(String custId) {
		SqlSession session = database.getSession();
		boolean flag = false;

		String result = null;
		try {
			result = session.selectOne("dbConfig.CustomerMapper.isExist",
					custId);
		} finally {
			if (session != null)
				session.close();
		}
		if (result != null)
			flag = true;

		return flag;
	}

	@Override
	public boolean loginCheck(String custId, String password) {
		SqlSession session = database.getSession();
		boolean flag = false;
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("custId", custId);
		param.put("password", password);
		try {
			String result = session.selectOne(
					"dbConfig.CustomerMapper.loginCheck", param);
			if (result != null)
				flag = true;
		} finally {
			if (session != null)
				session.close();
		}
		return flag;
	}

	@Override
	public boolean loginIDCheck(String custId) {
		boolean flag = false;
		SqlSession session = database.getSession();
		String result = null;

		try {
			result = session.selectOne("dbConfig.CustomerMapper.isExist",
					custId);
		} finally {
			if (session != null)
				session.close();
		}
		if (result != null)
			flag = true;

		return flag;
	}

	@Override
	public String lookForCustId(String name, String ssn)
			throws RecordNotFoundException {
		SqlSession session = database.getSession();
		HashMap<String, String> info = new HashMap<String, String>();
		info.put("custName", name);
		info.put("custSsn", ssn);
		String custId = null;
		try {
			custId = session.selectOne("dbConfig.CustomerMapper.lookForCustId",
					info);
		} finally {
			if (session != null)
				session.close();
		}
		if (custId == null)
			throw new RecordNotFoundException(custId);
		return custId;
	}

	@Override
	public String getPasswordQuestion(String custId)
			throws RecordNotFoundException {
		SqlSession session = database.getSession();
		String passwordQuestion = null;
		try {
			passwordQuestion = session.selectOne(
					"dbConfig.CustomerMapper.getPasswordQuestion", custId);
		} finally {
			if (session != null)
				session.close();
		}
		if (passwordQuestion == null)
			throw new RecordNotFoundException(custId);

		return passwordQuestion;
	}

	@Override
	public String lookForPassword(String custId, String passwordQuestion,
			String passwordAnswer) throws RecordNotFoundException {
		SqlSession session = database.getSession();
		HashMap<String, String> info = new HashMap<String, String>();
		info.put("custId", custId);
		info.put("passwordQuestion", passwordQuestion);
		info.put("passwordAnswer", passwordAnswer);
		String password = null;
		try {
			password = session.selectOne(
					"dbConfig.CustomerMapper.lookForPassword", info);
		} finally {
			if (session != null)
				session.close();
		}
		if (password == null)
			throw new RecordNotFoundException(custId);
		return password;
	}

@Override
	public void update_cust_admin(Customer customer) throws RecordNotFoundException {
		if (!isExist(customer.getCustId()))
			throw new RecordNotFoundException(customer.getCustId());

		SqlSession session = database.getSession();
		
		
		
		System.out.println("update_cust_admin:"+customer);
		
		try {
			session.update(
					"dbConfig.CustomerMapper.update_cust_admin", customer);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

	}


	@Override
	public void update_cust(String custId, String custEmail,
			String phoneNumber,
			String passwordQuestion, String passwordAnswer)
			throws RecordNotFoundException {

		HashMap<String, String> info = new HashMap<>();
		info.put("custId", custId);
		info.put("custEmail", custEmail);
		info.put("phoneNumber", phoneNumber);
		info.put("passwordQuestion", passwordQuestion);
		info.put("passwordAnswer", passwordAnswer);

		if (!isExist(custId))
			throw new RecordNotFoundException(custId);

		SqlSession session = database.getSession();

		try {
			session.update("dbConfig.CustomerMapper.update_cust", info);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

	}

	@Override
	public void update_pass(String custId, String password)
			throws RecordNotFoundException {
		HashMap<String, String> info = new HashMap<>();
		info.put("custId", custId);
		info.put("password", password);

		if (!isExist(custId))
			throw new RecordNotFoundException(custId);

		SqlSession session = database.getSession();

		try {
			session.update("dbConfig.CustomerMapper.update_pass", info);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

	}

	

	@Override
	public ArrayList<Customer> permissionCustList(int custLevel) {
		ArrayList<Customer> custList = new ArrayList<Customer>();
		SqlSession session = database.getSession();
		try {
			custList = (ArrayList) session
					.selectList("dbConfig.PermissionMapper.showPermissionCust",custLevel);
		} finally {
			if (session != null)
				session.close();
		}
		return custList;
	}
	
	@Override
	public ArrayList<Customer> AllPermissionCustList() {
		ArrayList<Customer> custList = new ArrayList<Customer>();
		SqlSession session = database.getSession();
		//System.out.println("CustomerDAOImple : AllPermissionCustList");
		try {
			custList = (ArrayList) session
					.selectList("dbConfig.PermissionMapper.showAllPermissionCust");
		} finally {
			if (session != null)
				session.close();
		}
		return custList;
	}
	
	
	

	@Override
	public void update_custPermission(String custId, int custLevel)throws RecordNotFoundException {
		if (!isExist(custId))
			throw new RecordNotFoundException(custId);

		SqlSession session = database.getSession();
		// Integer level = new Integer(custLevel);
		// HashMap<String,> info = new HashMap<String, String>();
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("custId", custId);
		param.put("custLevel", custLevel);
		
		try {
			session.update(
					"dbConfig.PermissionMapper.update_custPermission", param);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
		
	}

	@Override
	public ArrayList<Customer> getCustomerList(int custLevel) {
		ArrayList<Customer> custList = new ArrayList<Customer>();
		SqlSession session = database.getSession();
		if(custLevel ==0)
			custList = (ArrayList) session.selectList("dbConfig.CustomerMapper.getAllCustomers",custLevel);	
		else
			custList = (ArrayList) session.selectList("dbConfig.CustomerMapper.getCustomerList",custLevel);
		
		return custList;
	}

}